package com.menu.Controller;

import com.menu.Pojo.Goods;
import com.menu.Service.GoodsService;
import com.menu.Service.OrderService;
import com.menu.Service.UserService;
import com.menu.Utils.CommonUtil;
import com.menu.Utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


@Api(tags = "管理员接口")
@RestController
@RequestMapping("/Admin")
@Slf4j
public class AdminController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CommonUtil commonUtil;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @ApiOperation("添加商品")
    @PostMapping("/addGoods")
    public ResponseResult addGoods(Goods goods,@RequestPart("multipartFile") MultipartFile multipartFile){
        if (goods == null || multipartFile == null || multipartFile.isEmpty()){
            return ResponseResult.error("参数错误！");
        }
        Integer []goodsPrice = null;
        if (goods.getGoodsPrice() == null
                || (goodsPrice = commonUtil.verifyMoney(goods.getGoodsPrice())) == null ){
            return ResponseResult.error("商品价格不合法！");
        }
        if(!commonUtil.verifyStirng(goods.getGoodsName())){
            return ResponseResult.error("商品名称不合法！");
        }
        if (!commonUtil.verifyStirng(goods.getGoodsDecription())){
            return ResponseResult.error("商品描述不合法！");
        }
        if (!commonUtil.verifyInteger(goods.getGoodsStore())){
            return ResponseResult.error("商品库存不合法！");
        }
        if (!commonUtil.verifyInteger(goods.getGoodsLimit())){
            return ResponseResult.error("商品限购不合法！");
        }
        if (goods.getGoodsLimit() > goods.getGoodsStore()){
            return ResponseResult.error("商品限购必须大于库存！");
        }
        if (!commonUtil.verifyStirng(goods.getGoodsClassify())
                || !commonUtil.verifyClassfy(goods.getGoodsClassify())){
            return ResponseResult.error("商品分类不合法！");
        }
        if (!(goods.getState() == 0 || goods.getState() == 1)){
            return ResponseResult.error("商品状态有误！");
        }
        if (!commonUtil.verifyInteger(goods.getGoodsSales())){
            return ResponseResult.error("商品销量有误！");
        }
        goods.setGoodsPriceInt(goodsPrice[0]);
        goods.setGoodsPriceDec(goodsPrice[1]);
        return goodsService.addGoods(goods,multipartFile);
    }

    @ApiOperation("更改商品信息")
    @PutMapping("/modifyGoods")
    public ResponseResult modifyGoods(Goods goods,MultipartFile multipartFile){
        if (goods == null){
            return ResponseResult.error("参数错误！");
        }
        if (goods.getGoodsId() == null){
            return ResponseResult.error("商品id不合法！");
        }
        Integer []goodsPrice = null;
        if (goods.getGoodsPrice() == null
                || (goodsPrice = commonUtil.verifyMoney(goods.getGoodsPrice())) == null ){
            return ResponseResult.error("商品价格不合法！");
        }
        if(!commonUtil.verifyStirng(goods.getGoodsName())){
            return ResponseResult.error("商品名称不合法！");
        }
        if (!commonUtil.verifyStirng(goods.getGoodsDecription())){
            return ResponseResult.error("商品描述不合法！");
        }
        if (!commonUtil.verifyInteger(goods.getGoodsStore())){
            return ResponseResult.error("商品库存不合法！");
        }
        if (!commonUtil.verifyInteger(goods.getGoodsLimit())){
            return ResponseResult.error("商品限购不合法！");
        }
        if (goods.getGoodsLimit() > goods.getGoodsStore()){
            return ResponseResult.error("商品限购必须大于库存！");
        }
        if (!commonUtil.verifyStirng(goods.getGoodsClassify())
                || !commonUtil.verifyClassfy(goods.getGoodsClassify())){
            return ResponseResult.error("商品分类不合法！");
        }
        if (!(goods.getState() == 0 || goods.getState() == 1)){
            return ResponseResult.error("商品状态有误！");
        }
        if (!commonUtil.verifyStirng(goods.getGoodsSurfaceimg())){
            return ResponseResult.error("商品封面图为空！");
        }
        if (!commonUtil.verifyInteger(goods.getGoodsSales())){
            return ResponseResult.error("商品销量有误！");
        }
        goods.setGoodsPriceInt(goodsPrice[0]);
        goods.setGoodsPriceDec(goodsPrice[1]);
        return goodsService.modifyGoods(goods,multipartFile);
    }

    @ApiOperation("根据商品id查找")
    @GetMapping("/selectGoodsById")
    public ResponseResult selectGoodsById(Integer goodsId, @ApiIgnore HttpServletRequest httpRequest){
        if (goodsId == null){
            return ResponseResult.error("参数有误！");
        }
        return goodsService.selectGoodsById(goodsId,1, httpRequest);
    }

    @ApiOperation("商品分页查询")
    @GetMapping("/selectGoodsByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classfify",value = "商品分类,为空的时候 默认为所有"),
            @ApiImplicitParam(name = "state",value = "商品状态 为空的时候默认所有")
    })
    public ResponseResult selectGoodsByPage(Integer pageStart,Integer pageSize,Integer state,String classfify){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (state != null && !(state == 0 || state == 1)){
            return ResponseResult.error("参数有误！");
        }
        return goodsService.selectGoodByPage(pageStart,pageSize,state,classfify);
    }

    @ApiOperation("模糊搜索")
    @GetMapping("/visualGoods")
    public ResponseResult visualGoods(Integer pageStart,Integer pageSize,String Information){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (!commonUtil.verifyStirng(Information)){
            return ResponseResult.error("参数为空！");
        }
        return goodsService.visualGoods(Information,pageStart,pageSize,null);
    }

    @ApiOperation("商家退款")
    @PostMapping("/resolveRefound")
    public ResponseResult resolveRefound(Long orderId){
        if (orderId == null){
            return ResponseResult.error("订单号有误！");
        }
        return orderService.resolveRefound(orderId);
    }

    @ApiOperation("用户分页查询")
    @GetMapping("/selectUserByPage")
    public ResponseResult selectUserByPage(Integer pageStart,Integer pageSize){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        return userService.selectUserByPage(pageStart,pageSize);
    }

    @ApiOperation("模糊搜索")
    @GetMapping("/visualUser")
    public ResponseResult visualSearch(Integer pageStart,Integer pageSize,String userInformation){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (!commonUtil.verifyStirng(userInformation)){
            return ResponseResult.error("参数为空！");
        }
        return userService.visualSearch(userInformation,pageStart,pageSize);
    }

    @ApiOperation("根据用户Id查找")
    @GetMapping("/selectByUserId")
    public ResponseResult selectByUserId(Integer userId){
        if (userId == null){
            return ResponseResult.error("参数有误！");
        }
        return userService.selectByUserId(userId);
    }

    @ApiOperation("根据用户Id封禁")
    @GetMapping("/banUserId")
    @ApiImplicitParam(name = "state",value = "0 无事 1封禁")
    public ResponseResult banUserId(Integer userId,Integer state){
        if (userId == null){
            return ResponseResult.error("参数有误！");
        }
        if (state == null || !(state == 1 || state == 0)){
            return ResponseResult.error("参数有误！");
        }
        return userService.banUserId(userId,state);
    }

}
